/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.j4sc.submission;

import java.io.StringWriter;

import javanet.staxutils.IndentingXMLStreamWriter;

import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;

import com.googlecode.j4sc.PartyInformation;

class SubmissionMetaData {

	private enum Tag {
		REQUEST("request"),
		ACCOUNT_ID("account-id"),
		REFERENCE("reference"),
		APP_NAME("app-name"),
		INFORMATION("information"),
		CALLING_PARTY("calling-party"),
		CALLED_PARTY("called-party"),
		NAME("name"),
		IDENTIFIER("identifier");
		
		private final String tagName;

		private Tag(String tagName) {
			this.tagName = tagName;
		}

		private String getTagName() {
			return tagName;
		}
	}
	
	private static class XmlBuilder {
		
		private XMLStreamWriter writer;
		private StringWriter out;

		private XmlBuilder() throws XMLStreamException {
			out = new StringWriter();
	        XMLOutputFactory factory = XMLOutputFactory.newInstance();
	        writer = new IndentingXMLStreamWriter(factory.createXMLStreamWriter(out));
		}

		private void startDocument(String version) throws XMLStreamException {
			writer.writeStartDocument(version);
		}
		
    	private void startElement(Tag tag) throws XMLStreamException {
    		writer.writeStartElement(tag.getTagName());
    	}

        private void element(Tag tag, String value) throws XMLStreamException {
            if (value != null) {
                startElement(tag);
                writer.writeCharacters(value);
                writer.writeEndElement();
            }
        }
        
        private void endElement() throws XMLStreamException {
        	writer.writeEndElement();
        }
        
        private void endDocument() throws XMLStreamException {
            writer.writeEndDocument();
            writer.flush();
            writer.close();        	
        }
        
        @Override
        public String toString() {
        	return out.toString();
        }
	}
	
	static String createXmlMetadata(String accountId, String applicationName, String reference, 
			PartyInformation information) throws XMLStreamException {
		
		XmlBuilder payload = new XmlBuilder();
		payload.startDocument("1.0");
        payload.startElement(Tag.REQUEST);

        payload.element(Tag.ACCOUNT_ID, accountId);
        payload.element(Tag.REFERENCE, reference);
        payload.element(Tag.APP_NAME, applicationName);
        
        if (information != null && !information.isEmpty()) {
        	payload.startElement(Tag.INFORMATION);
        	if (!information.isCallingPartyEmpty()) {
        		addParty(payload, Tag.CALLING_PARTY, information.getCallingPartyName(), 
        				information.getCallingPartyIdentifier());
        	}
        	if (!information.isCalledPartyEmpty()) {
        		addParty(payload, Tag.CALLED_PARTY, information.getCalledPartyName(), 
        				information.getCalledPartyIdentifier());
        	}
        	payload.endElement();
        }
        
        payload.endElement();
        payload.endDocument();

        return payload.toString();
	}

	private static void addParty(XmlBuilder payload, Tag tag, String partyName, String partyIdentifier) throws XMLStreamException {
		payload.startElement(tag);
		payload.element(Tag.NAME, partyName);
		payload.element(Tag.IDENTIFIER, partyIdentifier);
		payload.endElement();
	}
}
